package com.ljb.android.comm.router.core

import com.alibaba.android.arouter.base.UniqueKeyTreeMap

class InterceptGroup {


    private var mDefPriority = 0

    var mInterceptorsPriority: UniqueKeyTreeMap<Int, IRouterInterceptor> =
        UniqueKeyTreeMap("More than one interceptors use same priority [%s]")


    fun addInterceptor(interceptor: IRouterInterceptor, priority: Int = mDefPriority) {
        mInterceptorsPriority[priority] = interceptor
        if (priority == mDefPriority) {
            mDefPriority++
        }
    }

    fun removeInterceptor(interceptor: IRouterInterceptor): Boolean {
        var key: Int? = null
        mInterceptorsPriority.forEach {
            if (it.value == interceptor) {
                key = it.key
                return@forEach
            }
        }
        if (key != null) {
            return mInterceptorsPriority.remove(key, interceptor)
        }
        return false
    }

    fun getSize(): Int {
        return mInterceptorsPriority.size
    }

}